package com.qjc.demo.thread;

import java.util.concurrent.*;

/**
 * @Description: 有甲乙丙三个线程，甲线程输出A，已线程输出B，丙线程输出C，要求，同时启动三个线程按顺序输出ABABC，循环10次
 * @Author: qjc
 * @Date: 2020/3/25
 */
public class SortThread1SingleThreadExecutor {

    private final static int THREAD_POOL_SIZE = 1;
    private final static long KEEP_ALIVE_TIME = 0L;

    /**
     * 本题主要考察，多线程协同工作的问题，本身多线程是没有执行顺序的，所以可以用锁来控制线程顺序，但是对于本题一个单一线程池就能解决问题
     */

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
//        ExecutorService executorService = new ThreadPoolExecutor(THREAD_POOL_SIZE, THREAD_POOL_SIZE, KEEP_ALIVE_TIME,
//                TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(10), new ThreadPoolExecutor.AbortPolicy());

        Thread thread1 = new Thread(() -> System.out.println("A"));
        Thread thread2 = new Thread(() -> System.out.println("B"));
        Thread thread3 = new Thread(() -> System.out.println("C"));

        for (int i = 0; i < 10; i++) {
            executorService.submit(thread1);
            executorService.submit(thread2);
            executorService.submit(thread1);
            executorService.submit(thread2);
            executorService.submit(thread3);
        }
        executorService.shutdown();

    }
}




